package edu.buaa.act.travel.m.tpdl;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xml.sax.Parser;

public class TripItinerary implements JSONable, Serializable{

	private static final long serialVersionUID = 1L;
	
	private Long id;
	
	private String name;
	
	private Activity initial;
	
	private List<Activity> activities = null;
	private Map<String, Activity> idMap = new HashMap<String, Activity>();
	
	private TripItinerary(){
		
	}
	
	
	public Activity getActivity(String id){
		return idMap.get(id);
	}
	
	public void addActivity(Activity a){
		idMap.put(a.getId(), a);
		a.setItinerary(this);
	}
	
	public List<Activity> getAllActivitiesInOrder(){
		if(activities==null){
			activities = new ArrayList<Activity>();
//			activities.add(initial);
			Flow flow = initial.getDefaultOutgoing();
			Activity activity ;
			while(flow != null){
				activity = flow.getTarget();
				activities.add(activity);
				flow = activity.getDefaultOutgoing();
			}
			if(activities.size()>0){
				activities.remove(activities.size()-1);
			}
		}
		return activities;
	}
	
	/**
	 * 为手机端专门写的方法
	 * @return
	 */
	public List<TpdlActivity> getAllActivitiesInOrder2(){
		this.getAllActivitiesInOrder();
		List<TpdlActivity> list = new ArrayList<TpdlActivity>();
		for(Activity a: activities){
			if(a instanceof TpdlActivity){
				list.add((TpdlActivity) a);
			}
		}
		return list;
	}
	
	@Override
	public String toJSONString() {
		JSONObject o = new JSONObject();
		try {
			o.put("id", this.id);
			o.put("name", this.name);
			o.put("activities", getAllActivitiesInOrder2());
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return o.toString();
	}
	
	public JSONObject toJSONObject(){
		JSONObject o = new JSONObject();
		try {
			o.put("id", this.id);
			o.put("name", this.name);
			o.put("activities", getAllActivitiesInOrder());
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return o;
	}

	static public TripItinerary parseJSON(JSONObject j){
		TripItinerary itinerary = new TripItinerary();
		try {
			itinerary.fromJSON(j);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return itinerary;
	}
	
	@Override
	public void fromJSON(JSONObject j) throws JSONException {
		if(j.has("id")){
			this.setId(j.getLong("id"));
		}
		this.setName(j.getString("name"));
		
		ActivityFactory factory = ActivityFactory.getInstance();
		// 临时数组
		List<Activity> tempActivities = new ArrayList<Activity>();
		
		// 加上开始活动
		StartActivity start = new StartActivity("wholestart");
		this.addActivity(start);
		this.setInitial(start);
		tempActivities.add(start);
		
		JSONArray array = j.getJSONArray("activities");
		for(int i = 0; i< array.length(); i++ ){
			JSONObject o = array.getJSONObject(i);
			Activity a = factory.create(o.getString("type"));
			a.fromJSON(o);
			this.addActivity(a);
			tempActivities.add(a);
		}
		EndActivity end = new EndActivity("wholeend");
		this.addActivity(end);
		tempActivities.add(end);
		
		// 建立Flow
		for(int k = 0; k<tempActivities.size()-1; k++){
			Flow flow = tempActivities.get(k).createOutFlow("jsonflow");
			tempActivities.get(k+1).addIncomingFlow(flow);
		}
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Activity getInitial() {
		return initial;
	}

	public void setInitial(Activity initial) {
		this.initial = initial;
	}

	public Map<String, Activity> getIdMap() {
		return idMap;
	}
	

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
}
